package com.me.modules.questionnaire.service.impl;

import com.me.modules.questionnaire.dao.AnCheckboxDao;
import com.me.modules.questionnaire.dao.AnRadioDao;
import com.me.modules.questionnaire.entity.AnCheckboxEntity;
import com.me.modules.questionnaire.entity.AnRadioEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.me.modules.questionnaire.dao.QuSurveyAnswerDao;
import com.me.modules.questionnaire.entity.QuSurveyAnswerEntity;
import com.me.modules.questionnaire.service.QuSurveyAnswerService;



@Service
public class QuSurveyAnswerServiceImpl implements QuSurveyAnswerService {
	@Autowired
	private QuSurveyAnswerDao quSurveyAnswerDao;
	@Autowired
	private AnRadioDao radioDao;
	@Autowired
	private AnCheckboxDao checkboxDao;

	@Override
	public QuSurveyAnswerEntity queryObject(Integer id){
		return quSurveyAnswerDao.queryObject(id);
	}

	@Override
	public List<QuSurveyAnswerEntity> queryList(Map<String, Object> map){
		return quSurveyAnswerDao.queryList(map);
	}

    @Override
    public List<QuSurveyAnswerEntity> queryListByBean(QuSurveyAnswerEntity entity) {
        return quSurveyAnswerDao.queryListByBean(entity);
    }

	@Override
	public int queryTotal(Map<String, Object> map){
		return quSurveyAnswerDao.queryTotal(map);
	}

	@Override
	public int save(QuSurveyAnswerEntity quSurveyAnswer){


		return quSurveyAnswerDao.save(quSurveyAnswer);
	}

	@Override
	public int update(QuSurveyAnswerEntity quSurveyAnswer){
        return quSurveyAnswerDao.update(quSurveyAnswer);
	}

	@Override
	public int delete(Integer id){
        return quSurveyAnswerDao.delete(id);
	}

	@Override
	public int deleteBatch(Integer[] ids){
        return quSurveyAnswerDao.deleteBatch(ids);
	}

	@Override
	public int saveAnswer(QuSurveyAnswerEntity surveyAnswer, Map<String, Map<String, Object>> quMaps) {
		Date curDate=new Date();

		//保存答案信息
		int surveyId=surveyAnswer.getSurveyId();

//		Survey survey=(Survey) session.get(Survey.class, surveyId);
//		SurveyDirectory survey=(SurveyDirectory) session.get(SurveyDirectory.class, surveyId);
//		System.out.println("survey:"+survey);
//		Integer answerNum = survey.getAnswerNum();
//		if(answerNum==null){
//			answerNum=0;
//		}
//		survey.setAnswerNum(answerNum+1);
//		session.update(survey);//更新回答数
//		int surveyQuAnItemNum=survey.getAnItemLeastNum();//可以回答的最少项目数

		surveyAnswer.setBgAnDate(curDate);
		surveyAnswer.setEndAnDate(new Date());

		//计算答卷用时
		long time=surveyAnswer.getEndAnDate().getTime()-surveyAnswer.getBgAnDate().getTime();
		surveyAnswer.setTotalTime(Float.parseFloat(time/(60*60)+""));
		quSurveyAnswerDao.save(surveyAnswer);


		int anCount=0;
		//保存答案
		//单选题
		Map<String,Object> radioMaps=quMaps.get("radioMaps");
//		String surveyId=surveyAnswer.getSurveyId();
		int surveyAnswerId = surveyAnswer.getId();
		int answerQuCount = 0;
		if (radioMaps != null){
			for (String key : radioMaps.keySet()) {
				answerQuCount++;
				int quId = Integer.parseInt(key);
				int quItemId = (int) radioMaps.get(key);
				AnRadioEntity anRadio=new AnRadioEntity(surveyId,surveyAnswerId,quId,quItemId);
				radioDao.save(anRadio);
			}
		}

//		anCount+=saveAnRadioMaps(surveyAnswer, radioMaps,session);
		//多选题
		Map<String,Object> checkboxMaps=quMaps.get("checkboxMaps");
		if (checkboxMaps != null){
			for (String key : checkboxMaps.keySet()) {
				int quId = Integer.parseInt(key);
				Map<String, Object> map = (Map<String, Object>) checkboxMaps.get(key);
				for (String keyMap : map.keySet()) {
					answerQuCount++;
					int quItemId = Integer.parseInt(keyMap);
					AnCheckboxEntity anCheckbox=new AnCheckboxEntity(surveyId,surveyAnswerId,quId,quItemId);
					checkboxDao.save(anCheckbox);
				}
			}
		}

		anCount += answerQuCount;
		//填空题
//		Map<String,Object> fillblankMaps=quMaps.get("fillblankMaps");
//		anCount+=saveAnFillMaps(surveyAnswer, fillblankMaps,session);

		//保存anCount
		surveyAnswer.setCompleteItemNum(anCount);
		int isComplete=0;
		if(anCount >= 0){
			isComplete=1;//表示回完
		}
		surveyAnswer.setIsComplete(isComplete);
		int isEffective=0;
		if(anCount>0){
			isEffective=1;//暂时只要答过一题就表示回答有效
		}
		surveyAnswer.setIsEffective(isEffective);
		quSurveyAnswerDao.update(surveyAnswer);

		return 0;
	}

	@Override
	public int queryAnswerTotal(String id) {
		return quSurveyAnswerDao.queryAnswerTotal(id);
	}

}
